﻿Public Class frmGradientPanel

    Private Sub frmGradientPanel_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        'Set up data sources from FillStyle and FillGradientStyle combo boxes
        cboFillGradientStyle.DataSource = System.Enum.GetValues(GetType(System.Drawing.Drawing2D.LinearGradientMode))
        cboFillStyle.DataSource = System.Enum.GetValues(GetType(PolyMonControls.GradientPanel.BackgroundType.FillStyles))

        'Bind Form objects to Gradient Panel Properties
        cboFillGradientStyle.DataBindings.Add("SelectedItem", GradientPanel1.Background, "FillGradientStyle")
        cboFillStyle.DataBindings.Add("SelectedItem", GradientPanel1.Background, "FillStyle")
        pbBackColor1.DataBindings.Add("BackColor", GradientPanel1.Background, "BackColor1")
        pbBackColor2.DataBindings.Add("BackColor", GradientPanel1.Background, "BackColor2")

        nudBorderRadius.DataBindings.Add("Value", GradientPanel1.Border, "Radius")
        nudBorderWidth.DataBindings.Add("Value", GradientPanel1.Border, "Width")
        pbBorderColor.DataBindings.Add("BackColor", GradientPanel1.Border, "Color")

        'Add handlers for color selections
        AddHandler pbBackColor1.Click, AddressOf ChooseColor
        AddHandler pbBackColor2.Click, AddressOf ChooseColor
        AddHandler pbBorderColor.Click, AddressOf ChooseColor

        'Add Handler for Form object property changes
        AddHandler cboFillStyle.SelectedIndexChanged, AddressOf ControlValueChanged
        AddHandler cboFillGradientStyle.SelectedIndexChanged, AddressOf ControlValueChanged
        AddHandler pbBackColor1.BackColorChanged, AddressOf ControlValueChanged
        AddHandler pbBackColor2.BackColorChanged, AddressOf ControlValueChanged

        AddHandler nudBorderRadius.ValueChanged, AddressOf ControlValueChanged
        AddHandler nudBorderWidth.ValueChanged, AddressOf ControlValueChanged
        AddHandler pbBorderColor.BackColorChanged, AddressOf ControlValueChanged
    End Sub

    Private Sub ChooseColor(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Dim pb As PictureBox = CType(sender, PictureBox)
        With Me.ColorDialog1
            .Color = pb.BackColor
            Dim res As DialogResult = .ShowDialog()

            If res = Windows.Forms.DialogResult.OK Then
                pb.BackColor = .Color
            End If
        End With
    End Sub


    Private Sub ControlValueChanged(ByVal sender As Object, ByVal e As System.EventArgs)
        Dim Ctl As Control = CType(sender, Control)
        If Ctl.DataBindings.Count > 0 Then
            Ctl.DataBindings(0).WriteValue()
        End If
    End Sub

 
End Class